using System;
using System.Collections.Generic;
using UnityEngine;

namespace RedDotSystem
{
    public enum RedDotType
    {
        Normal,
        RedDotNodeNum,//数字红点
        RedDotDataNum,//数据红点
    }

    public class RedDotSystem : NormalSingleton<RedDotSystem>
    {
        /// <summary>
        /// 红点字典
        /// </summary>
        private Dictionary<RedDotDefine, RedDotTreeNode> mRedDotLogicDic = new();

        /// <summary>
        /// 初始化红点树
        /// </summary>
        /// <param name="nodeList"></param>
        public void InitlizateRedDotTree(List<RedDotTreeNode> nodeList)
        {
            foreach(var item in nodeList)
            {
                //Debug.Log($"")
                if (!mRedDotLogicDic.ContainsKey(item.node))
                {
                    mRedDotLogicDic.Add(item.node, item);
                }
            }
        }

        /// <summary>
        /// 更新红点状态
        /// </summary>
        /// <param name="redKey"></param>
        public void UpdateRedDotState(RedDotDefine redKey)
        {
            if(redKey == RedDotDefine.None)
            {
                return;
            }

            RedDotTreeNode redDotNode = null;
            if(mRedDotLogicDic.TryGetValue(redKey,out redDotNode))
            {
                redDotNode.RefreshRedDotState();
                UpdateRedDotState(redDotNode.parentNode);
            }
        }

        #region 注册解绑红点状态改变事件
        public void RegisterRedDotChangeEvent(RedDotDefine redKey, Action<RedDotType,bool,int> changeEvent)
        {
            RedDotTreeNode redDotNode = null;
            if(mRedDotLogicDic.TryGetValue(redKey,out redDotNode))
            {
                redDotNode.OnRedDotActiveChange += changeEvent;
            }
            else
            {
                Debug.LogError($"{redKey} not exits,please check key define is error");
            }
        }

        public void UnRegisterRedDotChangeEvent(RedDotDefine redKey, Action<RedDotType,bool,int> changeEvent)
        {
            RedDotTreeNode redDotNode = null;
            if(mRedDotLogicDic.TryGetValue(redKey,out redDotNode))
            {
                redDotNode.OnRedDotActiveChange -= changeEvent;
            }
            else
            {
                Debug.LogError($"{redKey} not exits,please check key define is error");
            }
        }
        #endregion

        /// <summary>
        /// 获取子节点红点个数
        /// </summary>
        public int GetChildNodeRedDotCount(RedDotDefine redKey)
        {
            int childRedDotCount = 0;
            ComputeChildRedDotCount(redKey, ref childRedDotCount);

            return childRedDotCount;
        }

        /// <summary>
        /// 计算子节点红点个数
        /// </summary>
        private void ComputeChildRedDotCount(RedDotDefine redKey,ref int childRedDotCount)
        {
            foreach(var item in mRedDotLogicDic.Values)
            {
                if(item.parentNode == redKey)
                {
                    item.RefreshRedDotState();
                    if (item.redDotActive)
                    {
                        childRedDotCount += item.redDotCount;
                        if(item.redDotType != RedDotType.RedDotDataNum)
                        {
                            ComputeChildRedDotCount(item.node, ref childRedDotCount);
                        }
                    }
                }
            }
        }
    }
}

